﻿//
// Copyright (c) 2011 David Díaz & Fran Pedreira
//
// This file is part of PlanSim.
//
// PlanSim is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PlanSim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PlanSim. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using PlanSim.Core;


namespace PlanSim.Interfaz
{
    public partial class FormCola : Form
    {
        ListView.ListViewItemCollection ItemCollection;
        String nameEditing="";

        /// <summary>
        /// Variable que nos indica si añadimo o editamos una cola o un dispositivo, ya que el 
        /// formulario es el mismo.
        /// </summary>
        public int mode;
        
        public FormCola(ListView.ListViewItemCollection lvic)
        {
            InitializeComponent();
            ItemCollection = lvic;
        }

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			// Se selecciona por defecto el primer valor del combo
            if (mode == 2)
            {
                nameEditing=tNombre.Text;
            }
            else
            {
                cb.SelectedIndex = 0;
            }
		}


        private void cb_TextChanged(object sender, EventArgs e)
        {
            switch (cb.SelectedIndex)
            {
                case 1://RR
					pRR.Visible = true;
                    break;

				default:
					pRR.Visible = false;
					tQuantum.Text = "0";
					break;
            }
        }

        private void tQuantum_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (Char.IsNumber(e.KeyChar) || Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            { 
                e.Handled = true; 
            }
            /*if (Char.IsLetter(e.KeyChar))
            {
              e.Handled = true; 
            }*/
        }

        private void bOk_Click(object sender, EventArgs e)
        {
            if (validaFormulario())
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private bool validaFormulario()
        {


            if (!validaTexto(tNombre.Text))
            {
                MessageBox.Show("Tiene que indicar un nombre válido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tNombre.Focus();
                return (false);
                
            }
            else if (buscaRepetidos())
            {
                MessageBox.Show("El nombre ya existe", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tNombre.Focus();
                return (false);
            }

            if (cb.SelectedIndex < 0)
            {
                MessageBox.Show("Tiene que indicar la estrategia", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cb.Focus();
                return (false);
                
            }
            else if (cb.Text=="RR") 
            {
                if ((tQuantum.Text.Length == 0) || (tQuantum.Text == "0"))
                {
                    MessageBox.Show("Con estrategia Round Robin has de indicar quantum mayor de cero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tQuantum.Focus();
                    return (false);

                }
                else
                {
                    if (!validaNumero(tQuantum.Text))
                    {
                        MessageBox.Show("Has de establecer un valor numérico válido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tQuantum.Focus();
                        tQuantum.Text.Remove(0);
                        return (false);

                    }
                }
                
            }

            
            return (true);
        
        }

        private bool validaNumero(string s)
        {
            bool auxReturn=false;
            string expresion = @"^[1-9][0-9]*";
             
            if (Regex.IsMatch(s, expresion, RegexOptions.None))
            {
                if (Regex.Replace(s, expresion, String.Empty).Length == 0)
                {
                    auxReturn = true;
                }
            }
            return (auxReturn);
        }

        private bool validaTexto(string s)
        {
            bool auxReturn = false;
            string expresion = @"^[A-Za-z][0-9A-Za-z]*";

            if (Regex.IsMatch(s, expresion, RegexOptions.None))
            {
                if (Regex.Replace(s, expresion, String.Empty).Length == 0)
                {
                    auxReturn = true;
                }
            }
            return (auxReturn);
        }

        private bool buscaRepetidos()
        {
           
            bool auxReturn=false;
            int i=0;
            while (i<ItemCollection.Count)
            {

                if ((tNombre.Text == ItemCollection[i].Text) && (nameEditing != tNombre.Text))
                {
                    auxReturn = true;
                    i = ItemCollection.Count;
                }
                else
                {
                    i++;
                }
            }

            return (auxReturn);

        }



  

        
    }
}
    